package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.ProductSalesService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ProductSalesForm;
import com.youlai.boot.system.model.query.ProductSalesQuery;
import com.youlai.boot.system.model.vo.ProductSalesVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 商品销售数据前端控制层
 *
 * @author xiheforever
 * @since 2025-05-12 23:29
 */
@Tag(name = "商品销售数据接口")
@RestController
@RequestMapping("/api/v1/product-sales")
@RequiredArgsConstructor
public class ProductSalesController  {

    private final ProductSalesService productSalesService;

    @Operation(summary = "商品销售数据分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:product-sales:query')")
    public PageResult<ProductSalesVO> getProductSalesPage(ProductSalesQuery queryParams ) {
        IPage<ProductSalesVO> result = productSalesService.getProductSalesPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增商品销售数据")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:product-sales:add')")
    public Result<Void> saveProductSales(@RequestBody @Valid ProductSalesForm formData ) {
        boolean result = productSalesService.saveProductSales(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取商品销售数据表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:product-sales:edit')")
    public Result<ProductSalesForm> getProductSalesForm(
        @Parameter(description = "商品销售数据ID") @PathVariable Long id
    ) {
        ProductSalesForm formData = productSalesService.getProductSalesFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改商品销售数据")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:product-sales:edit')")
    public Result<Void> updateProductSales(
            @Parameter(description = "商品销售数据ID") @PathVariable Long id,
            @RequestBody @Validated ProductSalesForm formData
    ) {
        boolean result = productSalesService.updateProductSales(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除商品销售数据")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:product-sales:delete')")
    public Result<Void> deleteProductSaless(
        @Parameter(description = "商品销售数据ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = productSalesService.deleteProductSaless(ids);
        return Result.judge(result);
    }
}
